package com.mmlc.meimei.lichengkongjian.classify;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.androidnetframe.utils.LogUtils;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.jsbridge.BridgeHandler;
import com.mmlc.meimei.lichengkongjian.jsbridge.CallBackFunction;
import com.mmlc.meimei.lichengkongjian.jsbridge.DefaultHandler;
import com.mmlc.meimei.lichengkongjian.jsbridge.NestedWebView;
import com.mmlc.utils.Constants;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;



/**
 * Created by Administrator on 2016/12/23.
 */

public class MeiQiWebFragment extends LazyFragment {

    NestedWebView webView;
    // 标志位，标志已经初始化完成。
    private boolean isPrepared;
    boolean isFirstInit = true;
    String url;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        View view = inflater.inflate(R.layout.fragment_manufacturer, container, false);
        //XXX初始化view的各控件
        webView = (NestedWebView)view.findViewById(R.id.webview);
        initView();
        isFirstInit = true;
        isPrepared = true;
        lazyLoad();
        return view;
    }
    public void initView() {
        Bundle bundle = getArguments();
        url = bundle.getString("url");
        webView.setDefaultHandler(new DefaultHandler());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDisplayZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setUserAgentString(webView.getSettings().getUserAgentString() + "mmlc.meiqi");
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setAppCachePath(Constants.DIR_CACHE);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
//        webView.requestFocus();
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        //跳转到某一分类
        webView.registerHandler("presale_goods_list", new BridgeHandler() {
                    @Override
                    public void handler(String s, CallBackFunction callBackFunction) {
                        try {
                            LogUtils.e("presale_goods_list", URLDecoder.decode(s.toString(), "utf-8"));
                            JSONObject json = new JSONObject(URLDecoder.decode(s.toString(), "utf-8"));

                            String cate_id = json.optString("cate_id");
                            if (!TextUtils.isEmpty(cate_id)){
                                MeiQiFastBuyActivity activity = (MeiQiFastBuyActivity)getActivity();
                                activity.changeFragment(cate_id);
                            } else
                                ToastUtils.showToast("数据错误");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (UnsupportedEncodingException e)

                        {
                            e.printStackTrace();
                        }
                    }
                }

        );

    }


    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
//        webView.onResume();
    }

    @Override
    protected void lazyLoad() {
        if(!isPrepared || !isVisible) {
            return;
        }
        if(isFirstInit) {
            if (!ViewUtils.isEmpty(url))
                webView.loadUrl(url);
            else
                ToastUtils.showToast("网络请求有误");
            isFirstInit = false;
        }

    }

}
